<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <template>
          <div class="dashboard-container">
            <div class="app-container">
              <el-card>
                <el-tabs>
                  <!-- 放置页签 -->
                  <el-tab-pane label="角色管理">
                    <!-- 新增角色按钮 -->
                    <el-row style="height:60px">
                      <el-button
                        icon="el-icon-plus"
                        size="small"
                        type="primary"
                        @click="showDialog = true"
                      >新增角色</el-button>
                    </el-row>
                    <!-- 表格 -->
                    <el-table :data="roles">
                      <!-- type='index' : 表示序号从 1 开始 -->
                      <el-table-column label="序号" width="120" type="index" />
                      <el-table-column label="角色名称" width="240" prop="name" />
                      <el-table-column label="描述" prop="description" />
                      <el-table-column label="操作">
                        <template slot-scope="scope">
                          <el-button size="small" type="success">分配权限</el-button>
                          <el-button size="small" type="primary">编辑</el-button>
                          <el-button size="small" type="danger" @click="delRoles(scope.row.id)">删除</el-button>
                        </template>
                      </el-table-column>
                    </el-table>

                    <el-row type="flex" justify="center" align="middle" style="height: 60px">
                      <!-- 分页组件 -->
                      <el-pagination
                        :current-page="q.page"
                        :page-sizes="[2, 3, 5, 10]"
                        :page-size="q.pagesize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total"
                        @size-change="hSizeChange"
                        @current-change="hCurrentChange"
                      />
                    </el-row>
                  </el-tab-pane>
                </el-tabs>
              </el-card>
            </div>
          </div>
        </template>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        title="编辑弹层"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="roleFormRules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoles, delRoleById } from '@/api/roles'

export default {
  data() {
    return {
      q: {
        page: 1, // 当前页数
        pagesize: 2 // 一页显示条数
      },
      total: 0, // 总页数
      roles: [], // 角色列表
      roleForm: {
        name: '',
        description: ''
      },
      showDialog: false,
      roleFormRules: {}
    }
  },
  created() {
    // 加载角色列表
    this.loadRoles()
  },
  methods: {
    // 加载角色列表
    async loadRoles() {
      try {
        const res = await getRoles(this.q)
        // console.log(res)
        this.total = res.data.total
        this.roles = res.data.rows
      } catch (error) {
        console.log('角色获取失败的原因:' + error)
      }
    },
    hSizeChange(val) {
      console.log('当前页显示' + val + '条')
      this.q.pagesize = val
      this.loadRoles()
    },
    hCurrentChange(val) {
      console.log('当前页是:' + val)
      this.q.page = val
      this.loadRoles()
    },
    // 删除角色
    delRoles(id) {
      // console.log(id)
      this.$confirm('确定要删除吗?', '提示', {
        type: 'warning'
      }).then(async() => {
        try {
          const res = await delRoleById(id)
          // console.log(res)
          this.$message.success(res.message)

          if (this.roles.length === 1 && this.q.page > 1) {
            this.q.page--
          }

          this.loadRoles()
        } catch (error) {
          console.log(error.message)
        }
      }).catch(() => {
        console.log(this.roles)
      })

      // const p = this.$confirm('确定要删除吗?', '提示', {
      //   type: 'warning'
      // })
      // // p.then
      //   .then((result) => {
      //     console.log(result)
      //   })
      // // p.catch
      //   .catch((e) => {
      //     console.log(e)
      //   })
      // console.log('打印p:' + p)
    },

    // 确定新增
    hSubmit() {}
  }
}
</script>

<style>

</style>
